package com.paic.mhis.hcpms.evaluation.common.biz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.common.workflow.biz.service.HcpmsWFService;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.evaluation.common.biz.service.EvaluationWorkFlowService;

@Component("evaluationWorkFlowService")
public class EvaluationWorkFlowServiceImpl implements EvaluationWorkFlowService {

	@Autowired
	private HcpmsWFService hcpmsWFService;

	@Override
	public String startWorkFlow(String taskNo, UserDetails user, String taskType)
			throws Exception {
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		List<Authority> authorityList = user.getAuthorities();
		List<String> role = new ArrayList<String>();
		for (Authority authority : authorityList) {
			role.add(authority.getAuthority());
		}
		vo.setRole(role);
		vo.setTaskNo(taskNo);
		vo.setTaskType(taskType);
		vo.setActCurrent(user.getUsername());
		return hcpmsWFService.startProcess(vo).getActCurrent();
	}

	@Override
	public String processWorkFlow(String taskNo, UserDetails user,
			Map<String, String> condition, String taskType) throws Exception {
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		List<Authority> authorityList = user.getAuthorities();
		List<String> role = new ArrayList<String>();
		for (Authority authority : authorityList) {
			role.add(authority.getAuthority());
		}
		vo.setRole(role);
		vo.setTaskNo(taskNo);
		vo.setTaskType(taskType);
		vo.setActCurrent(user.getUsername());
		if (condition != null) {
			vo.setParamValue(condition);
		}
		return hcpmsWFService.processFlow(vo).getActCurrent();
	}

	@Override
	public String processWorkFlow(String taskNo, UserDetails user,
			String nextUserName, Map<String, String> condition, String taskType)
			throws Exception {
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		List<Authority> authorityList = user.getAuthorities();
		List<String> role = new ArrayList<String>();
		for (Authority authority : authorityList) {
			role.add(authority.getAuthority());
		}
		vo.setRole(role);
		vo.setTaskNo(taskNo);
		vo.setTaskType(taskType);
		vo.setActCurrent(user.getUsername());
		if (condition != null) {
			vo.setParamValue(condition);
		}
		return hcpmsWFService.processFlow(vo).getActCurrent();
	}

	@Override
	public String processWorkFlow(String taskNo, UserDetails user,
			String nextUserName, Map<String, String> condition,
			String taskType, String nextActNo) throws Exception {
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		List<Authority> authorityList = user.getAuthorities();
		List<String> role = new ArrayList<String>();
		for (Authority authority : authorityList) {
			role.add(authority.getAuthority());
		}
		vo.setRole(role);
		vo.setTaskNo(taskNo);
		vo.setTaskType(taskType);
		vo.setActCurrent(user.getUsername());
		vo.setAssigner(nextUserName);
		vo.setNextActNo(nextActNo);
		if (condition != null) {
			vo.setParamValue(condition);
		}
		return hcpmsWFService.processFlow(vo).getActCurrent();
	}

	@Override
	public void closeWorkFlow(String taskNo, String taskType)
			throws Exception {
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		vo.setTaskNo(taskNo);
		vo.setTaskType(taskType);
		hcpmsWFService.deleteWorkFlow(vo);
		
	}

	
	
}
